package com.zte.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zte.sys.dao.ISysDao;
import com.zte.sys.pojo.*;
import com.zte.sys.service.ISysService;
import com.zte.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author luode
 * @version 1.0
 * @date 2022/10/02 星期日 17:43
 */
@Service
public class SysImpl implements ISysService {
    @Autowired
    private ISysDao sysDao;

    public void setSysDao(ISysDao sysDao) {
        this.sysDao = sysDao;
    }

    public ISysDao getSysDao() {
        return sysDao;
    }

    @Override
    public List<sysUsers> getByUser(sysUsers users) {
        return sysDao.getByUser(users);
    }

    @Override
    public List<sysDept> getByDept(sysDept dept) {
        return sysDao.getByDept(dept);
    }

    @Override
    public List<sysJob> getByJob(sysJob job) {
        return sysDao.getByJob(job);
    }

    @Override
    public PageModel<sysUsers> getByUser(sysUsers users, PageModel<sysUsers> pageModel) {
        pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize()<=0?8:pageModel.getPageSize());
        PageHelper.startPage(pageModel.getPageNum(),pageModel.getPageSize());
        List<sysUsers> userList = sysDao.getByUser(users);
        System.out.println(userList);
        PageInfo<sysUsers> pageInfo = new PageInfo<sysUsers>(userList);
        pageModel.setData(new ArrayList<sysUsers>(userList));
        pageModel.setTotalPage(pageInfo.getPages());
        pageModel.setTotalRows(pageInfo.getTotal());
//        System.out.println(userList.getClass());
        return pageModel;
    }

    @Override
    public int updateUser(sysUsers users) {
        return sysDao.updateUser(users);
    }

    @Override
    public int addUser(sysUsers users) {
        return sysDao.addUser(users);
    }

    @Override
    public sysUsers queryOne(String id) {
        return sysDao.queryOne(id);
    }

    @Override
    public PageModel<sysDept> getDepts(sysDept dept, PageModel<sysDept> pageModel) {
        pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize()<=0?1000:pageModel.getPageSize());
        PageHelper.startPage(pageModel.getPageNum(),pageModel.getPageSize());
        List<sysDept> depts = sysDao.getDepts(dept);
        System.out.println(depts);
        PageInfo<sysDept> pageInfo = new PageInfo<sysDept>(depts);
        pageModel.setData(new ArrayList<sysDept>(depts));
        pageModel.setTotalPage(pageInfo.getPages());
        pageModel.setTotalRows(pageInfo.getTotal());
//        System.out.println(userList.getClass());
        return pageModel;
    }

    @Override
    public List<sysDept> getDepts(sysDept dept) {
        System.out.println(sysDao.getDepts(dept));
        return sysDao.getDepts(dept);

    }

    /**
     * 根据部门查询职位
     *
     * @param deptName
     * @return
     */
    @Override
    public ArrayList<sysJob> getJob(String deptName) {
        return sysDao.getJob(deptName);
    }


    @Override
    public int addDept(sysDept dept) {
        return sysDao.addDept(dept);
    }

    @Override
    public List<sysProvince> queryProvince() {
        return sysDao.queryProvince();
    }

    @Override
    public List<sysCity> queryCity() {
        return sysDao.queryCity();
    }

    @Override
    public sysDept queryDept(String id) {
        return sysDao.queryDept(id);
    }
}
